#include "mainwindow.h"
#include "./ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //示波器初始化
    plot_init();

    //串口初始化
    serial_init();

    connect(serial,SIGNAL(readyRead()),this,SLOT(serial_Read()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::serial_init()
{
    serial = new MSerial();
    on_checkcom_clicked();
    ui->log->document()->setMaximumBlockCount(100);

}
void MainWindow::plot_init()
{
    MPlot *plot1 = new MPlot(ui->mainplot1);
    MPlot *plot2 = new MPlot(ui->mainplot2);
    MPlot *plot3 = new MPlot(ui->mainplot3);

    dataplot.append(plot1);
    dataplot.append(plot2);
    dataplot.append(plot3);

    //plot1->start();
    // plot2->start();
    // plot3->start();
}




void MainWindow::on_checkcom_clicked()
{
    ui->comid->clear();
    //通过QSerialPortInfo查找可用串口
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        ui->comid->addItem(info.portName());
    }
}


// if (serial.open(QIODevice::ReadOnly)) {
//     QObject::connect(&serial, &QSerialPort::readyRead, [&]() {
//         QByteArray data = serial.readAll();
//         parser.onSerialDataReceived(data);
//     });
// } else {
//     qDebug() << "Failed to open serial port:" << serial.errorString();
// }

void MainWindow::on_opencom_clicked()
{
    static bool isopen = false;

    if(isopen == false){
        QString buad = ui->baudnumber->currentText();
        QString comid = ui->comid->currentText();
        if(serial->openPort(comid,buad.toInt()) == true)
        {


            isopen = true;
            ui->opencom->setText("关闭串口");
            ui->log->append("串口打开成功");
        }
        else{
            ui->log->append("串口打开失败");
        }
    }
    else{
        ui->opencom->setText("打开串口");
        isopen = false;
        serial->closePort();
        ui->log->append("串口关闭成功");
    }
}


void MainWindow::on_clearrcv_clicked()
{
    ui->log->clear();
}


void MainWindow::on_clearsend_clicked()
{
    ui->sendbox->clear();
}

void MainWindow::serial_Read()
{


    QString buffer_1;
    QByteArray buffer = serial->readAll();
    parser.onSerialDataReceived(buffer);

    dataplot[0]->append(parser.value1);
    dataplot[1]->append(parser.value2);
    dataplot[2]->append(parser.value3);
    if(!buffer.isEmpty())
    {

        //转换成16进制大写
        QString str=buffer.toHex().data();
        str=str.toUpper();


        //一个16进制占4位，8位为一字节，所以每两位16进制空一格
        for(int i=0;i<str.length();i+=2)
        {
            QString str_1 = str.mid (i,2);
            buffer_1 += str_1;
            buffer_1 += " ";
        }
        //读取之前显示数据
        QString receive = ui->log->toPlainText();
        //清空显示
        ui->log->clear();
        //重新显示
        if(ui->hexrcv->checkState() != Qt::Checked){
            receive += QString(buffer);
            ui->log->append(receive);
        }//直接显示
        else    {
            receive += QString(buffer_1);
            ui->log->append(receive);
        }//16进制显示
    }
    buffer.clear();
}


